package ipe.dealer;

import java.util.ArrayList;
import java.util.Random;



class GoodRow {
	String GoodName;
	String CityName;
	int Quantity;
	int Price;
	
	public GoodRow(String Good, String City, int initPrice, int initQuantity){
		this.GoodName = Good;
		this.CityName = City;
		this.Price = initPrice;
		this.Quantity = initQuantity;
	}
	
	void RandomizeQuantity(){
		Random rnd = new Random();
		int delta = rnd.nextInt(Quantity);
		Quantity = Math.round(Math.abs(Quantity+delta)*CityList.getRandomSeed(CityName));
	}
	
	void RecalculatePrice(){
		//odskusat si vyvoj tejto funkcie v calcu na grafe a podla toho ju dat sem
		//10000*initialPrice/quantity
	}
}
	

public class GoodGrid {
	
   
  
  private ArrayList<GoodRow> DataGrid; 
  
  public GoodGrid(){
	  DataGrid = new ArrayList<GoodRow>();
	  PopulateData();
  }


  private void PopulateData(){
		

	  
	for (int g=0; g < GoodList.Goods.length; g++){
	  for (int c=0; c < CityList.Cities.length; c++){
		  Random RndGen = new Random(CityList.CityRandomSeed[c]);
		  
		  DataGrid.add(new GoodRow(
				  				GoodList.Goods[g], 
				  				CityList.Cities[c], 
				  				(int) (GoodList.Prices[g]*CityList.PriceMods[c]*RndGen.nextGaussian()), 
				  				10
				  			)
		  );
	  }
    }
  }
  
  void ShowCity(String city){
	  for (int i=0; i < DataGrid.size(); i++){
		  if (DataGrid.get(i).CityName.equals(city)){
			  System.out.println(DataGrid.get(i).GoodName+";"+DataGrid.get(i).Quantity+";"+DataGrid.get(i).Price);
		  }
	  }
  }
  
  
  public ArrayList<GoodRow> getCity(String city){
	  ArrayList<GoodRow> CityGrid = new ArrayList<GoodRow>();
	  
	  for (int i=0; i < DataGrid.size(); i++){ // z tohto spravit endanced for loop
		  if (DataGrid.get(i).CityName.equals(city)){
			  CityGrid.add(DataGrid.get(i));
		  }
	  }
	  
	  return CityGrid;
  }
  

  //tieto for-cykly treba prepisat na while, aby sa neprehladaval cely grid, ale len pokial treba
  public int getUnitPrice(String cityName, String goodName){
	  
	  int retval=0;
	  
	  for (GoodRow item : DataGrid){
		  if (item.CityName.equals(cityName) && item.GoodName.equals(goodName)){
			  retval = item.Price;
			  break;			  
		  }
	  }
	  
	  
	  return retval;
  }
  
  public void removeGood(String cityName, String goodName, int amount){
	  for (int i=0; i < DataGrid.size(); i++){
		  if (DataGrid.get(i).CityName.equals(cityName) && DataGrid.get(i).GoodName.equals(goodName)){
			  DataGrid.get(i).Quantity -= amount;
			  break;
		  }
	  }
	  

  }


public void runEndOfDay() {

	/* 
	 * co ma robit tato funkcia:
	 * 1) zoberie kazdy goodrow:
	 * 1.1) nahodne prida/uberie mnozstvo (do hry vstupi city random seed (vid citylist))
	 * 1.2) na zaklade noveho mnozstva vypocita novu cenu (do hry vstupi city random seed (vid citylist))
	 */
	
	  for (GoodRow item : DataGrid){
		  //randomize amount
		  //calculate price
		  //tieto funkcie implementovat do GoodRow
		  item.RandomizeQuantity();
		  item.RecalculatePrice();
	  }	
	
}


  
  
}